<template>
  <!-- 弹框 -->
  <el-dialog :visible.sync="dialogShow" :title="title" @close="onClose">
    <!-- 表单区域 -->
    <el-form ref="form" label-width="140px" :model="formData" :rules="formRules">
      <el-form-item label="广告位名称" prop="advposdesc">
        <el-input v-model="formData.advposdesc" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="广告位描述" prop="advposname">
        <el-input v-model="formData.advposname" type="textarea" :rows="3" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="广告位尺寸" prop="advpossize">
        <el-input v-model="formData.advpossize" placeholder="请输入" />
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-button type="primary" size="small" @click="onSubmit">提交</el-button>
        <el-button size="small" @click="dialogShow = false">关闭</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import { addAdvPosInfoApi, updateAdvPosInfoApi } from '@/api/adv_pos'
export default {
  name: 'AddFormRos',
  data() {
    return {
      // 弹框控制变量
      dialogShow: false,
      // 表单数据变量
      formData: { advposdesc: '', advposname: '', advpossize: '' },
      // 表单验证规则
      formRules: {
        advposdesc: [{ required: true, message: '请输入', trigger: 'blur' }],
        advposname: [{ required: true, message: '请输入', trigger: 'blur' }],
        advpossize: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改' : '添加'
    }
  },
  methods: {
    // 提交行为
    async onSubmit() {
      // 表单的校验
      await this.$refs.form.validate()
      // 加工处理表单数据(拷贝一份再加工)   请求的发生
      this.formData.id
        ? await updateAdvPosInfoApi(this.formData)
        : await addAdvPosInfoApi(this.formData)
      // 提示效果
      this.$message.success(this.formData.id ? '修改成功' : '添加成功')
      this.dialogShow = false
      // 列表更新
      this.$emit('updateList')
    },
    // 弹框关闭
    onClose() {
      this.formData = { advposdesc: '', advposname: '', advpossize: '' }
      this.$refs.form.clearValidate()
    }
  }
}
</script>
<style scoped></style>
